Policies_Ui = Class.create({
    form_id_ : "search_form",
    form_container_ : "searchform_container",
    input_placeholder_ : "Titel durchsuchen",
    arr_cell_classes_ : new Array,
    arr_search_values_ : new Array,
    checksum_ : 0,
    cell_cnt_ : 0,
    hash_ : '',
    tmp_hash_ : '',
    reset_lock_ : false,
    initialize: function(class_name) 
    {
        if ($(this.form_id_)!=null)
        {
            if("onhashchange" in window || Prototype.Browser.Opera)
            {
                window.onhashchange = this.checkHash.bind(this);
                this.prepareTable();
                this.initSearchBox();
                if(window.location.hash.length > 0 && window.location.hash != 'none')
                    this.checkHash();
                this.hash_ = window.location.hash;
            }
            else
                return false;
        } 
        else
            return false;
    },
    checkHash : function()
    {
        if(this.hash_ != window.location.hash)
        {
            var arr_tmp, arr_param; 
            arr_tmp = window.location.hash.substr(1, window.location.hash.length).split("&");
            this.reset_lock_ = true;
            $(this.form_id_).reset();
            this.reset_lock_ = false;
            for(cnt=0; cnt < arr_tmp.length; ++cnt)
            {
                arr_param = arr_tmp[cnt].split("=");
                if(arr_param.length == 2)
                    $(arr_param[0]).value=arr_param[1];
            }
            this.updateTable();
        }
    },
    prepareTable: function()
    {
        $$('#datatable tr').each(function(row){
            var arr_header_cells = row.getElementsByTagName('th');
            for(cnt=0; cnt<arr_header_cells.length; ++cnt)
                this.arr_cell_classes_[cnt] = arr_header_cells[cnt].className;
            //console.log(arr_header_cells.length);
            this.cell_cnt_ = 0;
            $$('#datatable td').each(function(cell){
                var length = this.arr_cell_classes_.length;
                cell.addClassName(this.arr_cell_classes_[this.cell_cnt_++ % length]);
            }.bind(this));
        }.bind(this));
    },
    initSearchBox: function()
    {
        var searchfield = $('t_title');
        if((searchfield.placeholder === undefined && searchfield.placeHolder === undefined) || Prototype.Browser.IE)
            searchfield.value=this.input_placeholder_;
        searchfield.observe('click', function(event){event.findElement().value='';});
        $(this.form_id_).observe('submit', this.stopFormEvent.bindAsEventListener(this));
        $(this.form_id_).observe('reset', this.resetTable.bindAsEventListener(this));
        $(this.form_container_).setStyle({'display' : 'block'});
    },
    stopFormEvent : function(event)
    {
        event.preventDefault();
        this.updateTable();
    },
    updateTable : function()
    {
        this.tmp_hash_ = '';
        $$('#'+this.form_id_+' input[type=text]').each(function(element){
            this.addDataToSearchArray(element.value, element.id);
        }.bind(this));
        $$('#'+this.form_id_+' select').each(function(element){
            this.addDataToSearchArray(element.value, element.id);
        }.bind(this));
        window.location.hash = this.tmp_hash_;
        this.hash_ = window.location.hash;
        $$('#datatable tr').each(function(row){
            var arr_cells = row.getElementsByTagName('td');
            var tmp_checksum = 0;
            for(cnt=0; cnt<arr_cells.length; ++cnt)
            {
                var class_name = arr_cells[cnt].className;
                for(sub_cnt=0; sub_cnt<this.arr_search_values_.length;++sub_cnt)
                {
                    if(this.arr_search_values_[sub_cnt].getName() == class_name)
                    {
                        var re = new RegExp(this.arr_search_values_[sub_cnt].getValue(),"i");
                        if(!this.arr_search_values_[sub_cnt].isNegation() && arr_cells[cnt].innerHTML.search(re) != -1)
                            ++tmp_checksum;
                        if(this.arr_search_values_[sub_cnt].isNegation() && arr_cells[cnt].innerHTML.search(re) == -1)
                            ++tmp_checksum;
                    }
                }
            }
            if(tmp_checksum>=this.checksum_ || arr_cells.length==0)
                row.show();
            else
                row.hide();
        }.bind(this));
        this.arr_search_values_ = new Array();
        this.checksum_ = 0;
    },
    resetTable : function(event)
    {
        var searchfield = $('t_title');
        if((searchfield.placeholder === undefined && searchfield.placeHolder === undefined) || Prototype.Browser.IE)
            searchfield.value=this.input_placeholder_;
        $$('#datatable tr').each(function(row){
            row.show();
        });
        if(!this.reset_lock_)
        {
            window.location.hash = 'none';
            this.hash_ = window.location.hash;
            this.tmp_hash_ = '';
        }
    },
    addDataToSearchArray : function(search_value, class_name)
    {
        if(!search_value.empty() && search_value != 0 && search_value.indexOf(this.input_placeholder_) == -1)
        {
            if(!this.tmp_hash_.empty())
                this.tmp_hash_ += '&';
            this.tmp_hash_ += class_name+'='+search_value;
            this.arr_search_values_.push(new Policies_Ui_Search_Value(search_value, class_name));
            ++this.checksum_;
        }
    }
});
